﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using wdss.WebServices;

/*
 * ==================================================================================
 * Author: Sneha Nagendra / Anusha K U
 * Created On: 03-03-2011 23:59  IST
 * This page accepts user information and creates a new account.
 * ==================================================================================
 */


namespace wdss.GUI
{
    public partial class register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        /*protected void RoleList_DataBound(object sender, EventArgs e)
        {
            DropDownList RoleList = (DropDownList)FindControl("RoleList");
            //remove temp
            RoleList.Items.RemoveAt(0);
        }*/
        protected void btn_reg_Click(object sender, EventArgs e)
        {
            //lbl_Error = (Label)FindControl("lbl_Error");
            lbl_Error.Visible = false;
            //continue with add new user
            LogService logAction = new LogService();
           // DropDownList RoleList = (DropDownList)FindControl("RoleList");
            //DropDownList DepartmentList = (DropDownList)FindControl("DepartmentList");
            //TextBox txt_ConfirmPassword = (TextBox)FindControl("txt_ConfirmPassword");
            //TextBox txt_Request = (TextBox)FindControl("txt_Request");
            String password = Password.Text;
            String confirmPass = txt_ConfirmPassword.Text;
            //need to add one after we remove temp
            int role = RoleList.SelectedIndex + 1;
            int department = DepartmentList.SelectedIndex + 1;
            String username = Username.Text;
            String request = txt_Request.Text;
            String question = Question.Text;
            String answer = Answer.Text;
            String altEmail = Email.Text;

            RegisterService rs = new RegisterService();
            bool returnVal = rs.RegisterNewUser(username, password, confirmPass, request, role, department, altEmail, question, answer);
            //make user a temp role

            if (returnVal)
            {
                AddToDepartment newuser = new AddToDepartment();
                newuser.Add(Username.Text, department);
                
                /*
                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["aspnetdbConnectionString"].ConnectionString);
                SqlCommand cmd = new SqlCommand("select password from User1 where user1_name =" + username, con);
                try
                {
                    con.Open();
                    string pass = (string)cmd.ExecuteScalar();
                    con.Close();
                }
                catch (Exception ex)
                { }
                RsaEncryptionService rsa = new RsaEncryptionService(pass);
                string enc = rsa.encryption();
                try
                {
                    con.Open();
                    SqlCommand cmd1 = new SqlCommand("update  User1 set password = '" + enc + "'where user1_name =" + username, con);
                    cmd1.ExecuteNonQuery();
                    con.Close();
                }
                catch (Exception ex)
                { }

                */
                Username.Text = "";
                txt_ConfirmPassword.Text = "";
                Password.Text = "";
                txt_Request.Text = "";
                Question.Text = "";
                Answer.Text = "";
                Email.Text = "";
                logAction.LogAction(DateTime.Now.ToString() + ": A new user account has been created by the name of " + username + ".\n");
               // Label lbl_Result = (Label)cuw_Register.CompleteStep.ContentTemplateContainer.FindControl("lbl_Result");
                lbl_Result.Text = ("The User is created " + username).ToString() + ". Please wait for the system admin to grant you access.";
                lbl_Result.Visible = true;
                

            }
            else
            {
                logAction.LogAction(DateTime.Now.ToString() + ": A new user account failed to be created due to an error in RegisterService.\n");
               // Label lbl_Result = (Label)cuw_Register.CompleteStep.ContentTemplateContainer.FindControl("lbl_Result");
                lbl_Result.Text = ("The User is not created").ToString();
                lbl_Result.Visible = true;
            }
            

            //Server.Transfer("~/GUI/Login.aspx");

        }
    }
}
